//
//  UIImage+Extension.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/10/6.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit

extension UIImage {
    
    class func snapShotCurrentScreen() -> UIImage {
        let window = UIApplication.shared.keyWindow!
        //开启图片上下文   第三个参数自动调节比例
        UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, UIScreen.main.scale)
        //将当前的Window画到上下文中
        //它允许你截取一个UIView或者其子类中的内容，并且以位图的形式（bitmap）保存到UIImage中
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        
        //从图片上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图片上下文
        UIGraphicsEndPDFContext()
        
        return image!
    }
    
    
    
    //等比例压缩图片
    func  scaleImage(width : CGFloat) -> UIImage {
        
        let imageW = self.size.width
        let imageH = self.size.height
        
        if imageW < width {
            
            return self
        }
        
        //根据宽高比计算高度
        let scalH = imageH/imageW * width
        let imageBounds = CGRect(x: 0, y: 0, width: width, height: scalH)
        
        UIGraphicsBeginImageContextWithOptions(imageBounds.size, false, UIScreen.main.scale)
        self.draw(in: imageBounds)
        
         let image = UIGraphicsGetImageFromCurrentImageContext()
        //关闭图片上下文
        UIGraphicsEndPDFContext()
        
         return image! 
    }
    
    
}







